﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using cecbData;

namespace CECB
{
    /// <summary>
    /// Interaction logic for SimpleMatrixCalculation.xaml
    /// </summary>
    public partial class SimpleMatrixCalculation : Window
    {
        cecbData.DSSEntities datacontext;
        string projectReferance = String.Empty;
        Boolean isProjectSelected = false;
        public SimpleMatrixCalculation()
        {
            InitializeComponent();
            datacontext = new DSSEntities();
            ProjectAutocompleteTextBox.ItemsSource = datacontext.Projects;


        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {

        }

        private void ProjectAutocompleteTextBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }



        private void CalculateButton_MouseEnter(object sender, MouseEventArgs e)
        {
        }

        private void ProjectAutocompleteTextBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {


            Projects projObj = (Projects)ProjectAutocompleteTextBox.SelectedItem;
            try
            {
                projectReferance = projObj.proj_reference;
                isProjectSelected = true;
            }
            catch (Exception)
            {
                isProjectSelected = false;
                return;
            }
            clear();

            cecbClasses.Data_Adapters.MatrixSimple.SimpleMatrixCalculation simpleMatrixObj = new cecbClasses.Data_Adapters.MatrixSimple.SimpleMatrixCalculation();
            string isCorrect_CheckCorrectivity = simpleMatrixObj.CheckCorrectivity(projectReferance).ToString();

            // for the validation purpoces
            Boolean isCorrect1;
            Double value1;
            simpleMatrixObj.getPhysicalEnvironmentalAffection(projectReferance, out isCorrect1, out  value1);
            Boolean isCorrect2;
            Double value2;
            simpleMatrixObj.getEchologicallyAffectin(projectReferance, out isCorrect2, out  value2);
            Boolean isCorrect3;
            Double value3;
            simpleMatrixObj.getSociologicallyAffection(projectReferance, out isCorrect3, out  value3);

            // get the values to the text boxes
            textBox_total.Text = simpleMatrixObj.CheckCorrectivity(projectReferance).ToString();
            textBox_PhysicalImpact.Text = value1.ToString();
            textBox_EchologicalImpact.Text = value2.ToString();
            textBox_SocialImpact.Text = value3.ToString();

            bool isCorrectPrecentage1;
            double valPrecentage1;
            simpleMatrixObj.getSociologicallyAffectionPresentage(projectReferance, out isCorrectPrecentage1, out valPrecentage1);
            label_sociologicalImpactPresentage.Content = valPrecentage1.ToString();

            bool isCorrectPrecentage2;
            double valPrecentage2;
            simpleMatrixObj.getEchologicallyAffectinPresentage(projectReferance, out isCorrectPrecentage2, out valPrecentage2);
            label_EchologicalImpactPresentage.Content = valPrecentage2.ToString();
            bool isCorrectPrecentage3;
            double valPrecentage3;
            simpleMatrixObj.getPhysicalEnvironmentalAffectionPresentage(projectReferance, out isCorrectPrecentage3, out valPrecentage3);

            label_physicalImpactPresentage.Content = valPrecentage3.ToString();
            label_presentageMark1.Content = "%";
            label_presentageMark2.Content = "%";
            label_presentageMark3.Content = "%";




        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void ReportButton_Click(object sender, RoutedEventArgs e)
        {


            try
            {
                Projects projectObj = (Projects)ProjectAutocompleteTextBox.SelectedItem;
                if (projectObj.proj_name.ToString().Length > 0)
                {

                    new MetrixResultsCharts(projectObj.proj_reference.ToString()).ShowDialog();
                }



            }
            catch (Exception)
            {
                if (!isProjectSelected)
                {

                    MessageBox.Show("select a project first", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    MessageBox.Show("this function is currently unavailavle due to no values assign to this project", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }

        }


        public void clear()
        {

            textBox_total.Clear();
            textBox_PhysicalImpact.Clear();
            textBox_EchologicalImpact.Clear();
            textBox_SocialImpact.Clear();
            label_sociologicalImpactPresentage.Content = "";

            label_EchologicalImpactPresentage.Content = "";
            label_physicalImpactPresentage.Content = "";
            label_presentageMark1.Content = "";
            label_presentageMark2.Content = "";
            label_presentageMark3.Content = "";

        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (!isProjectSelected)
            {
                MessageBox.Show("select a project first", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                new Matrix.SimpleMatrixFinalResult_Window(projectReferance);
            }
        }

        private void ButtonClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }


    }
}
